package com.bkgy.jobs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.controller.lzDaNew.entity.LzDaNew;
import com.bkgy.controller.lzDaNew.mapper.ILzDaNewMapper;
import com.bkgy.modular.tpdodatatcm2280.dao.TPdoDataTcm2280Dao;
import com.bkgy.modular.tpdodatatcm2280.entity.TPdoDataTcm2280;
import com.bkgy.modular.tpdodatatcm2280.entity.TPdoDataTcm2280SumReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 15:26 2025/8/4
 */
@Slf4j
@Service
@Transactional
public class ProcessRollerJobs {

    @Autowired
    ILzDaNewMapper lzDaNewDao;

    @Autowired
    TPdoDataTcm2280Dao tPdoDataTcm2280Dao;

    public void run() {
        List<LzDaNew> list= lzDaNewDao.getLzDaNewByStatus();
        TPdoDataTcm2280SumReq tPdoDataTcm2280SumReq=tPdoDataTcm2280Dao.gettcm2280_pdo_Sum();
        if(tPdoDataTcm2280SumReq!=null){
            for(int i=0;i<list.size();i++){
                double rollingKilometers= Double.parseDouble(StringUtils.isEmpty(list.get(i).getRollingKilometers())?"0":list.get(i).getRollingKilometers())+tPdoDataTcm2280SumReq.getOutMatLenSum();
                double rollingTonnage= Double.parseDouble(StringUtils.isEmpty(list.get(i).getRollingTonnage())?"0":list.get(i).getRollingTonnage())+tPdoDataTcm2280SumReq.getOutMatWtSum();
                list.get(i).setRollingKilometers(String.valueOf(rollingKilometers));
                list.get(i).setRollingTonnage(String.valueOf(rollingTonnage));
                LambdaQueryWrapper<LzDaNew> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(LzDaNew::getID,list.get(i).getID());
                queryWrapper.eq(LzDaNew::getStatus,3);
                int flag =lzDaNewDao.update(list.get(i),queryWrapper);
                if(flag!=1){
                    throw new RuntimeException("工艺辊信息更新失败！");
                }
            }
        }
        List<TPdoDataTcm2280> pdoDataTcm2280List=tPdoDataTcm2280Dao.getTPdoDataTcm2280ByLzFlag();
        for(int j=0;j<pdoDataTcm2280List.size();j++){
            pdoDataTcm2280List.get(j).setLzFlag(1);
            LambdaQueryWrapper<TPdoDataTcm2280> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TPdoDataTcm2280::getExtid,pdoDataTcm2280List.get(j).getExtid());
            int flag =tPdoDataTcm2280Dao.update(pdoDataTcm2280List.get(j),queryWrapper);
            if(flag!=1){
                throw new RuntimeException("酸轧生产数据表状态更新失败！");
            }
        }

    }

}
